from PyQt4.QtGui import *
from PyQt4.QtCore import *
from ftdb.controls.document import DocumentSelector
from ftdb.gui.viewers.personnel import PersonnelSelector
from ftdb.controls.rich_text import RichTextEdit
import os

class ProjectViewer(QWidget):
    def __init__(self, dbconn=None, project_id=None, parent=None):
        super(ProjectViewer, self).__init__(parent)
        
        self._dbconn = dbconn
        self._project_id = project_id
        
        self._lm_ProjectViewer = QGridLayout()
        
        self._pnlProjectViewerMetadata = QWidget()
        self._lm_ProjectViewerMetadata = QGridLayout()
        
        self._splMetadataSections = QSplitter()
        
        # Project Information
        self._pnlProjectInformation = QGroupBox("Project Information")
        self._lm_ProjectInformation = QGridLayout()
        self._lblAircraft = QLabel("Aircraft")
        self._cboAircraft = QComboBox()
        self._lblProjectTitle = QLabel("Project Title")
        self._txtProjectTitle = QLineEdit()
        self._lblSeparationEngineer = QLabel("Separation Engineer")
        self._cboSeparationEngineer = PersonnelSelector(group="SEPARATION ENGINEER")
        self._lblProjectManager = QLabel("Project Manager")
        self._cboProjectManager = PersonnelSelector(group="PROJECT MANAGER")
        self._lblTestEngineer = QLabel("Test Engineer")
        self._cboTestEngineer = PersonnelSelector(group="TEST ENGINEER")
        self._lblRevision = QLabel("Revision")
        self._txtRevision = QLineEdit()
        
        # Test Event Information
        self._pnlTestEventInformation = QGroupBox("Test Event Information")
        self._lm_TestEventInformation = QGridLayout()
        self._lblSERequest = QLabel("SEEK EAGLE Request")
        self._txtSERequest = QLineEdit()
        self._lblJonWbs = QLabel("JON-WBS")
        self._cboJonWbs = QComboBox()
        self._lblWorkRequest = QLabel("Work Request")
        self._txtWorkRequest = QLineEdit()
        self._lblCertRecommendation = QLabel("Cert. Recommendation")
        self._txtCertRecommendation = QLineEdit()
        self._lblTestReport = QLabel("Test Report")
        self._txtTestReport = DocumentSelector()
        self._lblFlightClearance = QLabel("Flight Clearance")
        self._cboFlightClearance = QComboBox()
        
        # Mission Summary Information
        self._pnlMissionSummaryInformation = QGroupBox("Mission Summary Information")
        self._lm_MissionSummaryInformation = QGridLayout()
        self._lblStoresReleased = QLabel("List of Stores Released")
        self._lstStoresReleased = QListView()
        self._lblMsnSummariesFile = QLabel("Mission Summaries File")
        self._txtMsnSummariesFile = DocumentSelector()
        
        # Store Inventory Information
        self._pnlStoreInventoryInformation = QGroupBox("Store Count")
        self._lm_StoreInventoryInformation = QGridLayout()
        self._lblStoresAvailable = QLabel("Available:")
        self._txtStoresAvailable = QLineEdit()
        self._lblStoresRequired = QLabel("Required:")
        self._txtStoresRequired = QLineEdit()
        self._lblStoresUsed = QLabel("Used:")
        self._txtStoresUsed = QLineEdit()
        self._lblStoresRemaining = QLabel("Remaining:")
        self._txtStoresRemaining = QLineEdit()
        self._btnOpenStoreInventory = QPushButton("Open Store Inventory")
        
        # Mission Aborts Section
        self._pnlMissionAborts = QGroupBox("Mission Aborts")
        self._lm_MissionAborts = QGridLayout()
        self._lblGroundAborts = QLabel("Ground:")
        self._txtGroundAborts = QLineEdit()
        self._lblAirAborts = QLabel("Air:")
        self._txtAirAborts = QLineEdit()
        
        # Summary Information
        self._tabSummaryInformation = QTabWidget()
        self._txtProjectSummary = RichTextEdit()
        
        # Action Buttons
        self._pnlActionButtons = QWidget()
        self._lm_ActionButtons = QGridLayout()
        self._btnHelp = QPushButton()
        self._btnSubmit = QPushButton("Submit")
        self._btnCancel = QPushButton("Cancel")
        
        self._initCustomStyleSheets()
        self._initGui()
        
        self.show()
        self.activateWindow()
        self.raise_()
    
    def _initCustomStyleSheets(self):
        self._boldFont = QFont()
        self._boldFont.setBold(True)
        
        self._defaultFont = QFont()
        self._defaultFont.setBold(False)
        
        self._cssReadOnlyBg = """
            QLineEdit:read-only {
                background: #DDDDDD;
            }
        """
        
    def _initGui(self):
        self._initProjectInformationSection()
        self._initTestEventInformationSection()
        self._initMissionSummaryInformationSection()
        self._initStoreInventorySection()
        self._initMissionAbortsSection()
        self._initSummaryInformationSection()
        self._initActionButtonsSection()
        
        self._lm_ProjectViewerMetadata.addWidget(self._pnlProjectInformation, 0,0,1,3)
        self._lm_ProjectViewerMetadata.addWidget(self._pnlTestEventInformation, 1,0,2,1)
        self._lm_ProjectViewerMetadata.addWidget(self._pnlMissionSummaryInformation, 1,1,2,1)
        self._lm_ProjectViewerMetadata.addWidget(self._pnlStoreInventoryInformation, 1,2)
        self._lm_ProjectViewerMetadata.addWidget(self._pnlMissionAborts, 2,2)
        
        self._lm_ProjectViewerMetadata.setColumnStretch(0,1)
        self._lm_ProjectViewerMetadata.setColumnStretch(1,1)
        self._lm_ProjectViewerMetadata.setColumnStretch(2,0)
        self._lm_ProjectViewerMetadata.setMargin(0)
        
        self._pnlProjectViewerMetadata.setLayout(self._lm_ProjectViewerMetadata)
        self._pnlProjectViewerMetadata.setMinimumHeight(248)
        self._pnlProjectViewerMetadata.setMaximumHeight(248)
        
        self._splMetadataSections.addWidget(self._pnlProjectViewerMetadata)
        self._splMetadataSections.addWidget(self._tabSummaryInformation)
        self._splMetadataSections.setOrientation(Qt.Vertical)
        
        self._lm_ProjectViewer.addWidget(self._splMetadataSections, 0,0)
        self._lm_ProjectViewer.addWidget(self._pnlActionButtons, 1,0)
        
        self._lm_ProjectViewer.setRowStretch(0,1)
        
        self.setLayout(self._lm_ProjectViewer)
        self.setWindowTitle("Project")

    def _initProjectInformationSection(self):
        self._cboAircraft.setEditable(True)
        self._cboAircraft.lineEdit().setReadOnly(True)
        self._cboAircraft.setMinimumWidth(100)
        self._cboAircraft.setMaximumWidth(100)
        
        self._txtRevision.setMinimumWidth(75)
        self._txtRevision.setMaximumWidth(75)
        
        self._lm_ProjectInformation.addWidget(self._lblAircraft, 0,0)
        self._lm_ProjectInformation.addWidget(self._cboAircraft, 1,0)
        self._lm_ProjectInformation.addWidget(self._lblProjectTitle, 0,1)
        self._lm_ProjectInformation.addWidget(self._txtProjectTitle, 1,1)
        self._lm_ProjectInformation.addWidget(self._lblSeparationEngineer, 0,2)
        self._lm_ProjectInformation.addWidget(self._cboSeparationEngineer, 1,2)
        self._lm_ProjectInformation.addWidget(self._lblProjectManager, 0,3)
        self._lm_ProjectInformation.addWidget(self._cboProjectManager, 1,3)
        self._lm_ProjectInformation.addWidget(self._lblTestEngineer, 0,4)
        self._lm_ProjectInformation.addWidget(self._cboTestEngineer, 1,4)
        self._lm_ProjectInformation.addWidget(self._lblRevision, 0,5)
        self._lm_ProjectInformation.addWidget(self._txtRevision, 1,5)
        
        self._lm_ProjectInformation.setColumnStretch(0,0)
        self._lm_ProjectInformation.setColumnStretch(1,1)
        self._lm_ProjectInformation.setColumnStretch(2,1)
        self._lm_ProjectInformation.setColumnStretch(3,1)
        self._lm_ProjectInformation.setColumnStretch(4,1)
        self._lm_ProjectInformation.setColumnStretch(5,0)
        
        self._pnlProjectInformation.setLayout(self._lm_ProjectInformation)
        self._pnlProjectInformation.setFont(self._boldFont)
        
        for control in self._pnlProjectInformation.children():
            if isinstance(control, QWidget):
                control.setFont(self._defaultFont)
    
    def _initTestEventInformationSection(self):
        self._cboJonWbs.setEditable(True)
        self._cboJonWbs.lineEdit().setReadOnly(True)
        
        self._cboFlightClearance.setEditable(True)
        self._cboFlightClearance.lineEdit().setReadOnly(True)
        
        self._lm_TestEventInformation.addWidget(self._lblSERequest, 0,0)
        self._lm_TestEventInformation.addWidget(self._txtSERequest, 1,0)
        self._lm_TestEventInformation.addWidget(self._lblJonWbs, 0,1)
        self._lm_TestEventInformation.addWidget(self._cboJonWbs, 1,1)
        self._lm_TestEventInformation.addWidget(self._lblWorkRequest, 0,2)
        self._lm_TestEventInformation.addWidget(self._txtWorkRequest, 1,2)
        self._lm_TestEventInformation.addWidget(self._lblCertRecommendation, 2,0)
        self._lm_TestEventInformation.addWidget(self._txtCertRecommendation, 3,0)
        self._lm_TestEventInformation.addWidget(self._lblFlightClearance, 2,1,1,2)
        self._lm_TestEventInformation.addWidget(self._cboFlightClearance, 3,1,1,2)
        self._lm_TestEventInformation.addWidget(self._lblTestReport, 4,0,1,3)
        self._lm_TestEventInformation.addWidget(self._txtTestReport, 5,0,1,3)
        
        self._lm_TestEventInformation.setColumnStretch(0,1)
        self._lm_TestEventInformation.setColumnStretch(1,1)
        self._lm_TestEventInformation.setColumnStretch(2,1)
        
        self._pnlTestEventInformation.setLayout(self._lm_TestEventInformation)
        self._pnlTestEventInformation.setFont(self._boldFont)
        self._pnlTestEventInformation.setMinimumWidth(370)
        
        for control in self._pnlTestEventInformation.children():
            if isinstance(control, QWidget):
                control.setFont(self._defaultFont)
    
    def _initMissionSummaryInformationSection(self):
        self._lm_MissionSummaryInformation.addWidget(self._lblStoresReleased, 0,0)
        self._lm_MissionSummaryInformation.addWidget(self._lstStoresReleased, 1,0)
        self._lm_MissionSummaryInformation.addWidget(self._lblMsnSummariesFile, 2,0)
        self._lm_MissionSummaryInformation.addWidget(self._txtMsnSummariesFile, 3,0)
        
        self._pnlMissionSummaryInformation.setLayout(self._lm_MissionSummaryInformation)
        self._pnlMissionSummaryInformation.setFont(self._boldFont)
        
        for control in self._pnlMissionSummaryInformation.children():
            if isinstance(control, QWidget):
                control.setFont(self._defaultFont)
                
    def _initStoreInventorySection(self):
        self._txtStoresAvailable.setMinimumWidth(25)
        self._txtStoresAvailable.setMaximumWidth(25)
        
        self._txtStoresRequired.setReadOnly(True)
        self._txtStoresRequired.setStyleSheet(self._cssReadOnlyBg)
        self._txtStoresRequired.setMinimumWidth(25)
        self._txtStoresRequired.setMaximumWidth(25)
        
        self._txtStoresUsed.setReadOnly(True)
        self._txtStoresUsed.setStyleSheet(self._cssReadOnlyBg)
        self._txtStoresUsed.setMinimumWidth(25)
        self._txtStoresUsed.setMaximumWidth(25)
        
        self._txtStoresRemaining.setReadOnly(True)
        self._txtStoresRemaining.setStyleSheet(self._cssReadOnlyBg)
        self._txtStoresRemaining.setMinimumWidth(25)
        self._txtStoresRemaining.setMaximumWidth(25)
        
        self._lm_StoreInventoryInformation.addWidget(self._lblStoresAvailable, 0,0)
        self._lm_StoreInventoryInformation.addWidget(self._txtStoresAvailable,0,1)
        self._lm_StoreInventoryInformation.addWidget(self._lblStoresRequired, 0,2)
        self._lm_StoreInventoryInformation.addWidget(self._txtStoresRequired, 0,3)
        self._lm_StoreInventoryInformation.addWidget(self._lblStoresUsed, 1,0)
        self._lm_StoreInventoryInformation.addWidget(self._txtStoresUsed, 1,1)
        self._lm_StoreInventoryInformation.addWidget(self._lblStoresRemaining, 1,2)
        self._lm_StoreInventoryInformation.addWidget(self._txtStoresRemaining, 1,3)
        self._lm_StoreInventoryInformation.addWidget(self._btnOpenStoreInventory, 2,0,1,4)
        
        self._pnlStoreInventoryInformation.setLayout(self._lm_StoreInventoryInformation)
        self._pnlStoreInventoryInformation.setFont(self._boldFont)
        self._pnlStoreInventoryInformation.setMaximumWidth(275)
        
        for control in self._pnlStoreInventoryInformation.children():
            if isinstance(control, QWidget):
                control.setFont(self._defaultFont)
    
    def _initMissionAbortsSection(self):
        self._txtGroundAborts.setReadOnly(True)
        self._txtGroundAborts.setStyleSheet(self._cssReadOnlyBg)
        
        self._txtAirAborts.setReadOnly(True)
        self._txtAirAborts.setStyleSheet(self._cssReadOnlyBg)
        
        self._lm_MissionAborts.addWidget(self._lblGroundAborts, 0,0)
        self._lm_MissionAborts.addWidget(self._txtGroundAborts, 0,1)
        self._lm_MissionAborts.addWidget(self._lblAirAborts, 0,3)
        self._lm_MissionAborts.addWidget(self._txtAirAborts, 0,4)
        
        self._pnlMissionAborts.setLayout(self._lm_MissionAborts)
        self._pnlMissionAborts.setFont(self._boldFont)
        self._pnlMissionAborts.setMaximumWidth(200)
        
        for control in self._pnlMissionAborts.children():
            if isinstance(control, QWidget):
                control.setFont(self._defaultFont)
    def _initSummaryInformationSection(self):
        self._tabSummaryInformation.addTab(self._txtProjectSummary, "Project Summary")
    
    def _initActionButtonsSection(self):
        self._btnHelp.setIcon(QIcon(os.path.join(os.getenv("FTDBRESDIR"), "img/help.png")))
        
        self._lm_ActionButtons.addWidget(self._btnHelp, 0,0)
        self._lm_ActionButtons.addWidget(self._btnSubmit, 0,2)
        self._lm_ActionButtons.addWidget(self._btnCancel, 0,3)
        
        self._lm_ActionButtons.setColumnStretch(1,1)
        self._lm_ActionButtons.setMargin(0)
        
        self._pnlActionButtons.setLayout(self._lm_ActionButtons)
        
def main():
    from ftdb import env
    from ftdb.utils.dbutils import DatabaseAuthenticator, DatabaseConnectionGateway
    import sys
    
    env.initFTDBEnvironment()
    
    app = QApplication(sys.argv)
        
    dbconn = DatabaseConnectionGateway(DatabaseAuthenticator.getUserResponse())
    viewer = ProjectViewer(dbconn)
    
    app.exec_()

if __name__ == "__main__":
    main()